45G - Prime Problem - CodeForces Solution


number theory *2200

Please click on ads to support us..

Python Code:

def is_prime(val):
    if val <= 1:
        return False

    if val == 2:
        return True

    if (val & 1) == 0:
        return False

    d = 3
    while d*d <= val:
        if val % d == 0:
            return False
        d += 2
    return True

n = (int(input()))
f = [1]*(n+1)

def devide2prime(val):
    for i in range(2, n+1):
        if f[i] == 1 and is_prime(i) and is_prime(val - i):
            f[i] = 2
            return

tot = (n+1) * n // 2

def solve():
    if is_prime(tot):
        return

    if tot % 2 == 0:
        devide2prime(tot)
    else:
        if is_prime(tot - 2):
            f[2] = 2
            return

        f[3] = 3
        devide2prime(tot - 3)

solve()
for i in range(1, n+1):
    print(f[i], end = " ")


Comments

Submit
0 Comments
More Questions

63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even
1269A - Equation
441A - Valera and Antique Items
1702C - Train and Queries
816B - Karen and Coffee
838D - Airplane Arrangements
148B - Escape
847G - University Classes